package com.lzc.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.zhxy.mapper.TbStudentMapper;
import com.lzc.zhxy.pojo.LoginForm;
import com.lzc.zhxy.pojo.TbStudent;
import com.lzc.zhxy.service.TbStudentService;
import com.lzc.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 82389
* @description 针对表【tb_student】的数据库操作Service实现
* @createDate 2022-04-16 18:00:31
*/
@Service("tbStudentService")
@Transactional
public class TbStudentServiceImpl extends ServiceImpl<TbStudentMapper, TbStudent> implements TbStudentService {

    /**
     * 登陆方法
     * @param loginForm
     * @return
     */
    @Override
    public TbStudent login(LoginForm loginForm) {
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        TbStudent student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    /**
     * 根据id查找用户信息
     * @param intValue
     * @return
     */
    @Override
    public TbStudent getStudentById(int intValue) {
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<TbStudent>();
        queryWrapper.eq("id", intValue);

        TbStudent student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    /**
     * 按条件查询学生信息【带分页】
     * @param page
     * @param student
     * @return
     */
    @Override
    public IPage<TbStudent> getStudentByOpr(Page<TbStudent> page, TbStudent student) {
        QueryWrapper<TbStudent> queryWrapper = new QueryWrapper<>();
        if (student!=null){
            if (student.getClazzName()!=null){
                queryWrapper.eq("clazz_name",student.getClazzName());
            }
            if (student.getName()!=null){
                queryWrapper.eq("name",student.getName());
            }
            queryWrapper.orderByDesc("id");
            queryWrapper.orderByAsc("name");
        }
        IPage<TbStudent> selectPage = baseMapper.selectPage(page, queryWrapper);
        return selectPage;
    }
}
